//
//  CJExten+UITextField.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-07-05.
//

import UIKit
import Foundation

public extension UITextField {
    
    /// 快速创建UITextField
    @objc static func cj_textField(frame: CGRect = .zero,
                                   font: UIFont = .systemFont(ofSize: 16, weight: .regular),
                                   textColor: UIColor = .black,
                                   borderStyle: UITextField.BorderStyle = .none) -> UITextField {
        let tf = UITextField(frame: frame)
        tf.font = font
        tf.textColor = textColor
        tf.borderStyle = borderStyle
        return tf
    }
    
    /// 快速创建UITextField
    @objc static func cj_textField(frame: CGRect = .zero,
                                   font: UIFont = .systemFont(ofSize: 16, weight: .regular)) -> UITextField {
        let tf = cj_textField(frame: frame, font: font, textColor: .black)
        return tf
    }
    
    /// 设置font
    @objc func cj_font(_ font: UIFont) -> UITextField {
        self.font = font
        return self
    }
    
    /// 设置text
    @objc func cj_text(_ text: String) -> UITextField {
        self.text = text
        return self
    }
    
    /// 设置textColor
    @objc func cj_textColor(_ textColor: UIColor) -> UITextField {
        self.textColor = textColor
        return self
    }
    
    /// 设置attributedPlaceholder
    @objc func cj_setPlaceholder(_ text: String, color: UIColor?, font: UIFont?) -> UITextField {
        var dict: [NSAttributedString.Key : Any] = [:]
        if let color = color {
            dict[NSAttributedString.Key.foregroundColor] = color
        }
        if let font = font {
            dict[NSAttributedString.Key.font] = font
        }
        let attr = NSAttributedString(string: text, attributes: dict)
        self.attributedPlaceholder = attr
        return self
    }
    
}
